package syscommand

import (
	"errors"
	"strings"
    
    "collecter/app/common/enum"
    "collecter/app/common/helper"
)

type MakeQueueJob struct {
	JobName string
	BaseCmd
}

func NewMakeQueueJob() *MakeQueueJob {
	return &MakeQueueJob{}
}

func (qj *MakeQueueJob) Configure() {
	// fmt.Println("MakeCtl -> Configure")
	qj.StringVar(&qj.JobName, "job", "", "队列服务执行者名称，可以指定包名")
}

func (qj *MakeQueueJob) Execute() error {
	// code ...
	// fmt.Printf("%+v\n", mc)
	if len(qj.JobName) == 0 {
		return errors.New("队列服务执行者名称不能为空")
	}
	path, _, queueJob, parseErr := qj.ParsePathAndPkg(qj.JobName, "queueJob")
	if parseErr != nil {
		return parseErr
	}
	filename := path + helper.CamelString(queueJob, false) + ".go"
	// fmt.Println(filename)

	var queueJobTplStr string

	tplFile := enum.ROOT_DIR + "/console/tpl/queueJob.tpl"
	// fmt.Println("tplFile ========== ", tplFile)

	if err, val := helper.ReadFileContent(tplFile, false); err != nil {
		return err
	} else {
		queueJobTplStr = val
	}
	// fmt.Println("======= before ======== ", ctlTplStr)

	rootDir := strings.Trim(enum.ROOT_DIR, "/")
	dirs := strings.Split(rootDir, "/")
	// fmt.Println(rootDir)
	// fmt.Printf("=====dirs=========%+v\n", dirs)
	mod := dirs[len(dirs)-1]
	// fmt.Println("=====mod=========", mod, len(dirs))

	queueJobTplStr = strings.ReplaceAll(queueJobTplStr, "{{mod}}", mod)
	queueJobTplStr = strings.ReplaceAll(queueJobTplStr, "{{queueJobName}}", queueJob)
	// fmt.Println("========= after ========== ", ctlTplStr)

	if err := helper.WriteToFile(filename, queueJobTplStr); err != nil {
		return err
	}

	return nil
}
